package com.ruoyi.mentor.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.ExamineStatusConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mentor.bo.*;
import com.ruoyi.mentor.service.IExamineService;
import com.ruoyi.mentor.service.IInstructorService;
import com.ruoyi.mentor.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 导师Controller
 *
 * @author LH
 * @date 2021-05-21
 */
@Api(value = "导师控制器", tags = {"导师管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mentor/instructor")
public class InstructorController extends BaseController {

    private final IInstructorService iInstructorService;

    @Autowired
    private IExamineService examineService;

    /**
     * 查询导师列表
     */
    @ApiOperation("查询导师列表")
    @PreAuthorize("@ss.hasPermi('mentor:instructor:list')")
    @GetMapping("/list")
    public TableDataInfo<InstructorVo> list(@Validated InstructorQueryBo bo) {
        return iInstructorService.queryPageList(bo);
    }

    /**
     * 导出导师列表
     */
    @ApiOperation("导出导师列表")
    @PreAuthorize("@ss.hasPermi('mentor:instructor:export')")
    @Log(title = "导师", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(@Validated InstructorQueryBo bo) {
        List<ImportInstructorVo> list = iInstructorService.queryList(bo);
        ExcelUtil<ImportInstructorVo> util = new ExcelUtil<ImportInstructorVo>(ImportInstructorVo.class);
        return util.exportExcel(list, "导师信息");
    }

    /**
     * 获取导师详细信息
     */
    @ApiOperation("获取导师详细信息")
    @PreAuthorize("@ss.hasPermi('mentor:instructor:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iInstructorService.queryById(id));
    }

    /**
     * 新增导师
     */
    @ApiOperation("新增导师")
    @PreAuthorize("@ss.hasPermi('mentor:instructor:add')")
    @Log(title = "导师", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody InstructorAddBo bo) {
        return toAjax(iInstructorService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改导师
     */
    @ApiOperation("修改导师")
    @PreAuthorize("@ss.hasPermi('mentor:instructor:edit')")
    @Log(title = "导师", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@Validated @RequestBody InstructorEditBo bo) {
        return toAjax(iInstructorService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除导师
     */
    @ApiOperation("删除导师")
    @PreAuthorize("@ss.hasPermi('mentor:instructor:remove')")
    @Log(title = "导师" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iInstructorService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 添加导师详细信息
     */
    @ApiOperation("添加导师详细信息")
    @Log(title = "添加导师详细信息" , businessType = BusinessType.INSERT)
    @PostMapping("/addInstructorDetail")
    public AjaxResult addInstructorDetail(@RequestBody InstructorDetailVo instructorDetailVo) {
//        System.out.println(instructorDetailVo);
        iInstructorService.saveInstructorDetail(instructorDetailVo);
        return AjaxResult.success();
    }


    /**
     * 根据用户id获取导师详细信息
     */
    @ApiOperation("根据用户id获取导师详细信息")
    @GetMapping("/getInstructorDetailByUserId")
    public AjaxResult getInstructorDetailByUserId(@RequestParam("userId") Long userId) {
        InstructorDetailVo instructorDetailVo = iInstructorService.getInstructorDetailByUserId(userId);
        return AjaxResult.success(instructorDetailVo);
    }

    /**
     * 根据用户id获取导师详细信息
     */
    @ApiOperation("根据id获取导师详细信息")
    @GetMapping("/getInstructorDetailById")
    public AjaxResult getInstructorDetailById(@RequestParam("insId") Long insId) {
        InstructorDetailVo instructorDetailVo = iInstructorService.getInstructorDetailById(insId);
        return AjaxResult.success(instructorDetailVo);
    }


    /**
     * 管理员审核导师
     */
    @ApiOperation("管理员审核导师")
    @PostMapping("/examine")
    public AjaxResult examine(@RequestParam("adminName")String adminName,@RequestParam("status") Integer status,@RequestParam("insId") Long insId,@RequestParam("message") String message) {
        examineService.examineInstructor(adminName,status,insId,message);
        return AjaxResult.success();
    }


    @ApiOperation("获取该学生专业招生的老师，已经审核过的，招生未满的")
    @GetMapping("/accepted/list")
    public TableDataInfo<InstructorVo> getAccepted(@Validated StudentSelectInstructorBo bo){
        TableDataInfo<InstructorVo> res = iInstructorService.getAcceptedInstructorList(bo);
        return res;
    }

    @ApiOperation("获取所有导师信息")
    @GetMapping("/all")
    public AjaxResult all(){
        List<InsMapVo> insMapVos = iInstructorService.listVo(InsMapVo.class);
        return AjaxResult.success(insMapVos);
    }


    @ApiOperation("获取招生未招满的导师列表")
    @GetMapping("/selectNotFull")
    public AjaxResult selectNotFull(){
        List<NotFullInstructorVo> res = iInstructorService.selectNotFull();
        return AjaxResult.success(res);
    }






}
